﻿using System;
using System.Web;


using System.Text;
using System.Data;
using System.Xml;
using System.Web.SessionState;
using System.Web.Services;
using Gift.WebApp;
using System.Collections;

using Gift.DAL;
using Gift.Model;

namespace WebApp.cn.ashx
{
    public class commodityScoreHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        // Fields

        tbCommodityScore oCommodityScore = new tbCommodityScore();
        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (context.Session["user"] == null)
                {
                    context.Response.Write("0");
                }
                else if (context.Request.Form["id"].Contains(","))
                {
                    ArrayList list = ArrayList.Adapter(context.Request.Form["id"].Split(new char[] { ',' }));
                    foreach (string str in list)
                    {
                        this.oCommodityScore.user = (context.Session["user"] as tbUser).id;
                        this.oCommodityScore.goods = Convert.ToInt32(str);
                        this.oCommodityScore.scores = Convert.ToInt32(context.Request.Form["score"]);
                        BaseFactory.theCommodityScore.Add(this.oCommodityScore);
                    }
                    context.Response.Write("2");
                }
                else
                {
                    this.oCommodityScore.user = (context.Session["user"] as tbUser).id;
                    this.oCommodityScore.goods = Convert.ToInt32(context.Request.Form["id"]);
                    this.oCommodityScore.scores = Convert.ToInt32(context.Request.Form["score"]);
                    if (BaseFactory.theCommodityScore.Add(this.oCommodityScore) > 0)
                    {
                        double num;
                        StringBuilder builder = new StringBuilder();
                        DataTable shelvesGoodsById = BaseFactory.theGoods.GetShelvesGoodsById(this.oCommodityScore.goods);
                        if (shelvesGoodsById.Rows.Count > 0)
                        {
                            DataRow row = shelvesGoodsById.Rows[0];
                            if (!Convert.IsDBNull(row["scores"]))
                            {
                                num = Convert.ToDouble(row["scores"]);
                                if ((num < 1.5) && (num >= 1.0))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star01.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 2.0) && (num >= 1.5))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star015.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 2.5) && (num >= 2.0))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star02.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 3.0) && (num >= 2.5))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star025.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 3.5) && (num >= 3.0))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star03.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 4.0) && (num >= 3.5))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star035.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 4.5) && (num >= 4.0))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star04.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if ((num < 5.0) && (num >= 4.5))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star045.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                                else if (num == 5.0)
                                {
                                    builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star05.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                                }
                            }
                            else
                            {
                                builder.AppendLine(string.Concat(new object[] { "用户综合评分：<img src=\"", "/script/images/star00.gif\" /><span>(已有", row["scoreNumber"], "人评论)</span>" }));
                            }
                        }
                        StringBuilder builder2 = new StringBuilder();
                        DataTable commodityScoreByGoods = BaseFactory.theCommodityScore.GetCommodityScoreByGoods(this.oCommodityScore.goods);
                        builder2.AppendLine("<div>");
                        builder2.AppendLine("<p><em>宝贝与描述相符</em></p>");
                        builder2.AppendLine("<p>");
                        DataRow row2 = commodityScoreByGoods.Rows[0];
                        string str = row2["scores"].ToString();
                        if (!Convert.IsDBNull(row2["scores"]))
                        {
                            num = Convert.ToDouble(row2["scores"]);
                            if ((num >= 1.0) && (num < 1.5))
                            {
                                builder2.AppendLine("<strong>1</strong>分<img src=\"/script/images/star01.gif\" /></p>");
                            }
                            else if ((num >= 1.5) && (num < 2.0))
                            {
                                builder2.AppendLine("<strong>1.5</strong>分<img src=\"/script/images/star015.gif\" /></p>");
                            }
                            else if ((num >= 2.0) && (num < 2.5))
                            {
                                builder2.AppendLine("<strong>2</strong>分<img src=\"/script/images/star02.gif\" /></p>");
                            }
                            else if ((num >= 2.5) && (num < 3.0))
                            {
                                builder2.AppendLine("<strong>2.5</strong>分<img src=\"/script/images/star025.gif\" /></p>");
                            }
                            else if ((num >= 3.0) && (num < 3.5))
                            {
                                builder2.AppendLine("<strong>3</strong>分<img src=\"/script/images/star03.gif\" /></p>");
                            }
                            else if ((num >= 3.5) && (num < 4.0))
                            {
                                builder2.AppendLine("<strong>3.5</strong>分<img src=\"/script/images/star035.gif\" /></p>");
                            }
                            else if ((num >= 4.0) && (num < 4.5))
                            {
                                builder2.AppendLine("<strong>4</strong>分<img src=\"/script/images/star04.gif\" /></p>");
                            }
                            else if ((num >= 4.5) && (num < 5.0))
                            {
                                builder2.AppendLine("<strong>4.5</strong>分<img src=\"/script/images/star045.gif\" /></p>");
                            }
                            else if (num == 5.0)
                            {
                                builder2.AppendLine("<strong>5</strong>分<img src=\"/script/images/star05.gif\" /></p>");
                            }
                        }
                        else
                        {
                            builder2.AppendLine("<strong>0</strong>分<img src=\"/script/images/star00.gif\" /></p>");
                        }
                        builder2.AppendLine("<p>(已有<span>" + row2["scoresNumber"] + "</span>人进行评分)</p>");
                        builder2.AppendLine("</div>");
                        builder2.AppendLine("<div class=\"second_img\">");
                        if (!Convert.IsDBNull(row2["scores"]))
                        {
                            num = Convert.ToDouble(row2["scores"]);
                            if ((num >= 1.0) && (num < 1.5))
                            {
                                builder2.AppendLine("<strong style=\"right: 330px;\">1</strong>");
                            }
                            else if ((num >= 1.5) && (num < 2.0))
                            {
                                builder2.AppendLine("<strong style=\"right: 290px;\">1.5</strong>");
                            }
                            else if ((num >= 2.0) && (num < 2.5))
                            {
                                builder2.AppendLine("<strong style=\"right: 250px;\">2</strong>");
                            }
                            else if ((num >= 2.5) && (num < 3.0))
                            {
                                builder2.AppendLine("<strong style=\"right: 210px;\">2.5</strong>");
                            }
                            else if ((num >= 3.0) && (num < 3.5))
                            {
                                builder2.AppendLine("<strong style=\"right: 170px;\">3</strong>");
                            }
                            else if ((num >= 3.5) && (num < 4.0))
                            {
                                builder2.AppendLine("<strong style=\"right: 130px;\">3.5</strong>");
                            }
                            else if ((num >= 4.0) && (num < 4.5))
                            {
                                builder2.AppendLine("<strong style=\"right:90px;\">4</strong>");
                            }
                            else if ((num >= 4.5) && (num < 5.0))
                            {
                                builder2.AppendLine("<strong style=\"right: 50px;\">4.5</strong>");
                            }
                            else if (num == 5.0)
                            {
                                builder2.AppendLine("<strong style=\"right: 10px;\">5</strong>");
                            }
                        }
                        else
                        {
                            builder2.AppendLine("<strong style=\"right: 410px;\">0</strong>");
                        }
                        builder2.AppendLine("<img src=\"/script/images/score.gif\" alt=\"星级标尺\" />");
                        builder2.AppendLine("</div>");
                        builder2.AppendLine("<span class=\"clear\"></span>");
                        context.Response.Write(builder.ToString() + "+" + builder2.ToString());
                    }
                    else
                    {
                        context.Response.Write("1");
                    }
                }
            }
            catch
            {
                context.Response.Write("1");
            }
        }

        #endregion
    }
}
